﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace NakedSanta
{
    class Rotor : GameObject
    {
        private float rotationSpeed = -0.25f;
        private const int CENTER = 0;

        private const int NORTH_LEFT = 1;
        private const int NORTH_RIGHT = 2;

        private const int WEST_TOP = 3;
        private const int WEST_BOT = 4;

        private const int EAST_TOP = 5;
        private const int EAST_BOT = 6;

        private const int SOUTH_LEFT = 7;
        private const int SOUTH_RIGHT = 8;

        public Rotor(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;

            InitializeVertices();
            InitializeIndices();
            //SetNormals();
            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.WriteOnly);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColorNormal>(vertices);
            indexBuffer.SetData<short>(indices);
        }

        public override void Update(GameTime gameTime)
        {
            rotation *= Quaternion.CreateFromAxisAngle(Vector3.Up, rotationSpeed);
        }

        private void InitializeVertices()
        {
            vertices = new VertexPositionColorNormal[9];

            vertices[CENTER] = new VertexPositionColorNormal(new Vector3(0, 0, 0), Color.Red);

            vertices[NORTH_LEFT] = new VertexPositionColorNormal(new Vector3(-1f, 0, -5), Color.Red);

            vertices[NORTH_RIGHT] = new VertexPositionColorNormal(new Vector3(0f, 0, -5), Color.Red);
  

            vertices[WEST_TOP] = new VertexPositionColorNormal(new Vector3(-5, 0, 0f), Color.Red);
            vertices[WEST_BOT] = new VertexPositionColorNormal(new Vector3(-5, 0, 1f), Color.Red);

            vertices[EAST_TOP] = new VertexPositionColorNormal(new Vector3(5, 0, -1f), Color.Red);
            vertices[EAST_BOT] = new VertexPositionColorNormal(new Vector3(5, 0, 0f), Color.Red);

            vertices[SOUTH_LEFT] = new VertexPositionColorNormal(new Vector3(0f, 0, 5), Color.Red);
            vertices[SOUTH_RIGHT] = new VertexPositionColorNormal(new Vector3(1f, 0, 5), Color.Red);

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Normal = Vector3.Up;
            }
        }

        private void InitializeIndices()
        {
            indices = new short[24];

            //NORTH
            indices[0] = CENTER;
            indices[1] = NORTH_LEFT;
            indices[2] = NORTH_RIGHT;

            indices[5] = CENTER;
            indices[4] = NORTH_LEFT;
            indices[3] = NORTH_RIGHT;

            //EAST
            indices[6] = CENTER;
            indices[7] = EAST_TOP;
            indices[8] = EAST_BOT;

            indices[11] = CENTER;
            indices[10] = EAST_TOP;
            indices[9] = EAST_BOT;

            //SOUTH
            indices[12] = CENTER;
            indices[13] = SOUTH_RIGHT;
            indices[14] = SOUTH_LEFT;

            indices[17] = CENTER;
            indices[16] = SOUTH_RIGHT;
            indices[15] = SOUTH_LEFT;

            //WEST 
            indices[18] = CENTER;
            indices[19] = WEST_BOT;
            indices[20] = WEST_TOP;

            indices[23] = CENTER;
            indices[22] = WEST_BOT;
            indices[21] = WEST_TOP;
        }

        private void SetNormals()
        {
            for (int i = 0; i < indices.Length; i += 3)
            {
                Vector3 v1 = vertices[indices[i + 1]].Position - vertices[indices[i]].Position;
                Vector3 v2 = vertices[indices[i + 2]].Position - vertices[indices[i]].Position;
                Vector3 normal;
                Vector3.Cross(ref v2, ref v1, out normal);
                normal.Normalize();
                vertices[indices[i]].Normal += normal;
                vertices[indices[i + 1]].Normal += normal;
                vertices[indices[i + 2]].Normal += normal;
            }

            foreach (VertexPositionColorNormal v in vertices)
                v.Normal.Normalize();
        }
    }
}
